package org.jsola.hr.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.jsola.hr.dto.AttendGoOutDTO;
import org.jsola.hr.dto.AttendGoOutRecordAddDTO;
import org.jsola.hr.entity.AttendPunchDailyReportDO;
import org.jsola.hr.entity.EmpInfoDO;
import org.jsola.hr.query.AttendGoOutRecordQuery;
import org.jsola.hr.query.AttendPunchDailyReportQuery;
import org.jsola.hr.query.EmpInfoQuery;
import org.jsola.hr.service.IAttendGoOutRecordService;
import org.jsola.hr.service.IAttendGoOutService;
import org.jsola.hr.service.IAttendPunchDailyReportService;
import org.jsola.hr.service.IEmpInfoService;
import org.jsola.hr.vo.AttendGoOutVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 外出打卡
 * 
 * @author wxz
 *
 */
@Service("hrAttendGoOutServiceImpl")
public class AttendGoOutServiceImpl implements IAttendGoOutService {
	
    @Autowired
    private IAttendGoOutRecordService attendGoOutRecordService;
    @Autowired
    private IAttendPunchDailyReportService attendPunchDailyReportService;
	@Autowired
    private IEmpInfoService empInfoService;
	
	@Override
	public AttendGoOutVO punch(AttendGoOutDTO attendGoOutDTO, TokenUser tokenUser) {
		
    	// 查询员工信息
    	EmpInfoDO empInfoDO = getEmpInfoDO(tokenUser);
		
    	// 打卡时间
    	String punchTime = DateFormatUtils.format(new Date(), "HH:mm:ss");
    	// 年
    	String yearly = DateFormatUtils.format(new Date(), "yyyy");
    	// 月
    	String monthly = DateFormatUtils.format(new Date(), "MM");
    	// 日
    	String daily = DateFormatUtils.format(new Date(), "dd");
		
		AttendGoOutRecordAddDTO attendGoOutRecordAddDTO = new AttendGoOutRecordAddDTO();
		
		attendGoOutRecordAddDTO.setCompanyId(empInfoDO.getCompanyId());
		attendGoOutRecordAddDTO.setCompanyName("");
		attendGoOutRecordAddDTO.setDeptId("");
		attendGoOutRecordAddDTO.setDeptName("");
		attendGoOutRecordAddDTO.setEmpInfoId(empInfoDO.getId());
		attendGoOutRecordAddDTO.setEmpInfoName(empInfoDO.getName());
		attendGoOutRecordAddDTO.setYearly(yearly);
		attendGoOutRecordAddDTO.setMonthly(monthly);
		attendGoOutRecordAddDTO.setDaily(daily);
		attendGoOutRecordAddDTO.setPunchTime(punchTime);
		attendGoOutRecordAddDTO.setLocation(attendGoOutDTO.getLocation());
		attendGoOutRecordAddDTO.setLatitude(attendGoOutDTO.getLatitude());
		attendGoOutRecordAddDTO.setLongitude(attendGoOutDTO.getLongitude());
		attendGoOutRecordAddDTO.setOutInstru(attendGoOutDTO.getOutExplain());
		
		attendGoOutRecordService.save(attendGoOutRecordAddDTO, tokenUser);
		
		// 保存附件
		// TODO
		
		// 考勤打卡日报表统计
		AttendPunchDailyReportQuery attendPunchDailyReportQuery = new AttendPunchDailyReportQuery();
		
		attendPunchDailyReportQuery.setEmpInfoId(empInfoDO.getId());
		attendPunchDailyReportQuery.setYearly(yearly);
		attendPunchDailyReportQuery.setMonthly(monthly);
		attendPunchDailyReportQuery.setDaily(daily);
		
		List<AttendPunchDailyReportDO> reportDataList = attendPunchDailyReportService.selectDO(attendPunchDailyReportQuery, tokenUser.getSiteId());
		if (reportDataList.size() > 0) {
			
			AttendPunchDailyReportDO reportDO = reportDataList.get(0);
			
			AttendPunchDailyReportDO reportEntity = new AttendPunchDailyReportDO();
			reportEntity.setId(reportDO.getId());
			// 是否有外出 ; 1-是,0-否
			reportEntity.setIsGoOut(1);
			
			attendPunchDailyReportService.updateByIdSelective(reportEntity, tokenUser.getSiteId(), tokenUser.getUserId());
		}

		// 查询今日的外出记录
		AttendGoOutRecordQuery attendGoOutRecordQuery = new AttendGoOutRecordQuery();
		attendGoOutRecordQuery.setYearly(yearly);
		attendGoOutRecordQuery.setMonthly(monthly);
		attendGoOutRecordQuery.setDaily(daily);
		attendGoOutRecordQuery.setEmpInfoId(empInfoDO.getId());
		int counts = attendGoOutRecordService.selectCount(attendGoOutRecordQuery, tokenUser.getSiteId());
		counts += 1;

		AttendGoOutVO attendGoOutVO = new AttendGoOutVO();
		
		attendGoOutVO.setStatus(true);
		attendGoOutVO.setPunchTime(punchTime);
		attendGoOutVO.setLocation(attendGoOutDTO.getLocation());
		attendGoOutVO.setGoOutCounts(counts);
		
		return attendGoOutVO;
	}

	/**
	 * 查询员工信息
	 *
	 * @param tokenUser
	 * @return
	 */
	private EmpInfoDO getEmpInfoDO(TokenUser tokenUser) {

		// 查询员工信息
		EmpInfoQuery empInfoQuery = new EmpInfoQuery();
		empInfoQuery.setUserId(tokenUser.getUserId());
		EmpInfoDO empInfoDO = null;
		List<EmpInfoDO> empInfoDODataList =  empInfoService.selectDO(empInfoQuery, tokenUser);
		if (empInfoDODataList.size() > 0) {
			empInfoDO = empInfoDODataList.get(0);
		}
		return empInfoDO;
	}
}
